/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. biz
 * Email：service@phpcoo.com，phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/05/10 by OEdev $
*/
/* 前台、会员中心共用弹出 */

$(function(){
	//弹出赠送礼物框
	$(document).on("click", "[f='pop_send_gift']", function(){
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
		}
		else {
			$_touid = $(this).attr("data-touid");
			$width = $(this).attr("data-width");
			if (typeof($width) == "undefined") {
				$width = 800;
			}
			$height = $(this).attr("data-height");
			if (typeof($height) == "undefined") {
				$height = 510;
			}
			$_url = _ROOT_PATH+"index.php?m=user&c=gift&touid="+$_touid+"&r="+get_rndnum(8);
			oeShadeBox("pop_sendgift", "ajax", $_url, $width, $height);
		}
	});


	//弹出写信框
	$(document).on("click", "[f='pop_send_msg']", function(){
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
			return false;
		}
		else {
			$_touid = $(this).attr("data-touid");
			$width = $(this).attr("data-width");
			if (typeof($width) == "undefined") {
				$width = 850;
			}
			$height = $(this).attr("data-height");
			if (typeof($height) == "undefined") {
				$height = 550;
			}

			if ($_touid.length > 0) {
				$.ajax({
					type: "POST",
					url: _ROOT_PATH+"index.php?m=user&c=msgchat",
					cache: false,
					data: {a:"setsid", touid:$_touid, r:get_rndnum(8)},
					dataType: "json",
					success: function($data) {
						$json = eval($data);
						$response = $json.response;
						$result = $json.result;
						if ($response == "1") {
							if ($result.length > 0) {
								//打开聊天界面
								$chat_url = _ROOT_PATH+"index.php?m=user&c=msgchat&datatype=json&sid="+$result;
								oeChatShadeBox("pop_sendmsg", "ajax", $chat_url, $width, $height);
							}
						}
						else {
							if ($result.length > 0) {
								ToastShow($result);
							}
							else {
								ToastShow("获取数据失败");
							}
						}
					},
					error: function() {
						ToastShow("系统繁忙，请稍后再试");
					}
				});			
			}
		}
	});


	//弹出登录框JD
	$(document).on("click", "[f='pop_jdlogin']", function(){
		$width = $(this).attr("data-width");
		if (typeof($width) == "undefined") {
			$width = 420;
		}
		$height = $(this).attr("data-height");
		if (typeof($height) == "undefined") {
			$height = 280;
		}
		if (_CONNET_LOGIN == 1) {
			$height = 420;
		}
		/*
		$_url = _ROOT_PATH+"index.php?c=passport&a=jdlogin&r="+get_rndnum(8);
		oeShadeBox("pop_login", "ajax", $_url, $width, $height);
		*/
		_jdLogin($width, $height);
	
	});
	//JD登录ajax
	$(document).on("click", "[f='but_jdlogin_post']", function(){
		$loginname = $("#jd_loginname").val();
		$password = $("#jd_password").val();
		$loading = $(this).attr("data-loading");
		if (typeof($loading) == "undefined") {
			$loading = "";
		}

		if ($loginname.length == 0) {
			ToastShow("请输入用户名/邮箱/手机号");
			return false;
		}
		
		if ($password.length == 0) {
			ToastShow("请输入登录密码");
			return false;
		}
		if ($loading.length > 0) {
			$("#"+$loading).show();
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=passport",
			cache: false,
			data: {a:"loginpost", loginname:$loginname, password:$password, from:"top", r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") { //成功，返回页面
					window.location.reload();
				}
				else if ($response == "2") { //整合OEmarry
					window.top.location.href = $result;
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("登录失败，请检查帐号密码是否正确");
					}
					if ($loading.length > 0) {
						$("#"+$loading).hide();
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
	});

	//弹出举报窗
	$(document).on("click", "[f='pop_jubao']", function(){
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
			return false;
		}
		$home_uid = $(this).attr("data-id");
		$_url = _ROOT_PATH+"index.php?c=jubao&homeuid="+$home_uid+"&r="+get_rndnum(8);
		oeShadeBox("pop_jubao", "ajax", $_url, 600, 510);
	});
	//点击保存举报 AJAX
	$(document).on("click", "[f='but_save_jubao']", function(){
		$jubao_homeuid = $("#jubao_homeuid").val();
		$jubao_blacktype = $("#jubao_blacktype").val();
		$jubao_judes = $("#jubao_judes").val();
		$jubao_juimg = $("#jubao_juimg").val();
		$jubao_jumobile = $("#jubao_jumobile").val();
		$jubao_juemail = $("#jubao_juemail").val();

		if ($jubao_homeuid.length == 0) {
			ToastShow("举报对象UID错误");
			return false;
		}
		if ($jubao_blacktype.length == 0) {
			ToastShow("请选择举报类型");
			return false;
		}
		if ($jubao_judes.length == 0) {
			ToastShow("请填写举证描述");
			return false;
		}
		if ($jubao_jumobile.length == 0) {
			ToastShow("请填写举报人手机");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=jubao",
			cache: false,
			data: {
				a:"savejubao", homeuid:$jubao_homeuid, blacktype:$jubao_blacktype, judes:$jubao_judes, 
				juimg:$jubao_juimg, jumobile:$jubao_jumobile, juemail:$jubao_juemail, r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					_closeShadeBox("pop_jubao", function(){
						ToastShow("举报/投诉提交成功，请等待审核！");
					});

				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("提交失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
	});
	//举报上传图片
	$(document).on("change", "#file_jubao_img", function(){
		$(this).parent().addClass("loading");
		//上传图片
		$.ajaxFileUpload({
			"url": _ROOT_PATH+"index.php?m=user&c=index&a=upload&module=upload",
			"secureuri": false,
			"fileElementId": "file_jubao_img",
			"dataType": "json",
			"data": {uploadpart: "file_jubao_img"},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") { //上传成功
					$("#file_jubao_img").parent().removeClass("loading");
					$("#file_jubao_img").parent().find("img").attr("src", $imgurl).show();
					$("#file_jubao_img").parent().find("span").hide();
					$("#jubao_juimg").val($imgurl);
					_oeloveFileSynYun($attid); //同步云存储
				}
				//失败
				else {
					$error = "上传失败，请检查图片大小和格式！";
					if ($result.length > 0 ) {
						$error = $result;
					}
					ToastShow($error);
					$("#file_jubao_img").parent().removeClass("loading");
				}

			},
			error: function ($data, e, b) {
				ToastShow("上传失败，请检查图片大小和格式！");

			}
		});

	});

	//弹出委托红娘牵线
	$(document).on("click", "[f='pop_entrust_serv']", function(){
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
			return false;
		}
		$entrust_linetype = $(this).attr("data-linetype");
		$entrust_relid = $(this).attr("data-relid");
		$entrust_reload = $(this).attr("data-reload");
		if (typeof($entrust_reload) == "undefined") {
			$entrust_reload = "0";
		}
		$elem_id = $(this).attr("id"); //当前元素ID
		if (typeof($elem_id) == "undefined") {
			$elem_id = "";
		}
		$_url = _ROOT_PATH+"index.php?c=lineserv&linetype="+$entrust_linetype+
				"&relid="+$entrust_relid+"&reload="+$entrust_reload+"&elemid="+$elem_id+"&r="+get_rndnum(8);
		oeShadeBox("pop_entrust_serv", "ajax", $_url, 500, 320);
	});
	//保存委托服务 AJAX
	$(document).on("click", "[f='but_save_entrust_serv']", function(){
		$entrust_linetype = $(this).attr("data-linetype");
		$entrust_relid = $(this).attr("data-relid");
		$entrust_reload = $(this).attr("data-reload");
		if (typeof($entrust_reload) == "undefined") {
			$entrust_reload = "0";
		}
		$entrust_demand = $("#entrust_demand").val();

		if ($entrust_linetype.length == 0) {
			ToastShow("委托类型不能为空");
			return false;
		}
		if ($entrust_relid.length == 0) {
			ToastShow("委托关联信息不能为空");
			return false;
		}
		if ($entrust_demand.length == 0) {
			ToastShow("委托需求不能为空");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=lineserv",
			cache: false,
			data: {
				a:"saveentrust", linetype:$entrust_linetype, relid:$entrust_relid, demand:$entrust_demand, r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					_closeShadeBox("pop_entrust_serv", function(){
						ToastShow("委托申请成功，请等待审核！");
						if ($entrust_reload == "1") {
							setTimeout(function(){
								window.location.reload();
							}, 300);
						}
					});

				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("申请失败");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
	});
	
	//播放语音独白 2017.03.11
	$(document).on("click", "[f='but_play_sound']", function(){
		$_uid = $(this).attr("data-id");
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
			return false;
		}
		$this_obj = $(this);
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?c=home",
			cache: false,
			data: {
				a:"playsound", id:$_uid, r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					$("[f='but_play_sound']").removeClass("current");
					$this_obj.addClass("current");
					//播放
					$(".sound_music").remove();
					$("body").append('<embed id="sound_music" class="sound_music" loop="false" height="0" autostart="true" hidden="true" src="'+$result+'" />');
				}
				else if ($response == "2") {
					//录制提示
					$_url = _ROOT_PATH+"index.php?c=home&a=addsound&id="+$_uid+"&r="+get_rndnum(8);
					oeShadeBox("pop_add_sound", "ajax", $_url, 600, 500);
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("没有可播放的语音哦！");
					}
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});
	});

	//弹出录制语音窗口 2017.03.13
	$(document).on("click", "[f='but_open_addsound']", function(){
		$_uid = $(this).attr("data-id");
		if (_LOGIN_STATUS == 0) {
			_jdLogin();
			return false;
		}
		$_url = _ROOT_PATH+"index.php?c=home&a=addsound&id="+$_uid+"&r="+get_rndnum(8);
		oeShadeBox("pop_add_sound", "ajax", $_url, 600, 500);
	});

	
	//关闭消息提醒
	$(document).on("click", "[f='but_close_infowin']", function(){
		_closeInfoWinData();
	});
});
//jdlogin
function _jdLogin($width, $height) {
	if (typeof($width) == "undefined") {
		$width = 420;
	}
	if (typeof($height) == "undefined") {
		$height = 280;
	}
	if (_CONNET_LOGIN == 1) {
		$height = 420;
	}
	$_url = _ROOT_PATH+"index.php?c=passport&a=jdlogin&r="+get_rndnum(8);
	oeShadeBox("pop_login", "ajax", $_url, $width, $height);
}

//获取消息提醒
function _openInfoWinData() {
	if (_LOGIN_STATUS == 0) {
		_jdLogin();
		return false;
	}
	if ($("#infowin_box").length > 0) {
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=user",
			cache: false,
			data: {
				c:"infowin", r:get_rndnum(8)
			},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					if ($result.length > 0) {
						$("#infowin_data").html("");
						$("#infowin_data").html($result);
						$("#infowin_box").slideDown(300);

						setTimeout(function(){
							_closeInfoWinData();
						}, INFOWIN_CLOSETIME*1000);
					}
				}
				setTimeout(function(){
					_openInfoWinData();
				}, INFOWIN_LOOPTIME*1000);
				
			},
			error: function() {

			}
		});
	}
}
//关闭消息窗口
function _closeInfoWinData() {
	if ($("#infowin_box").length > 0) {
		$("#infowin_box").slideUp(300);
	}
}
